# python get-pip.py
# pip install Flask
# pip install Flask-Script
# pip install pandas
import json
import traceback
from flask import Flask
from flask import request
from flask_script import Manager

app = Flask(__name__)
manager = Manager(app)


@app.route("/", methods=["GET"])
def index():
    return "python service works!"


@app.route("/", methods=["POST"])
def execute_script():
    result = {"success": True}
    try:
        script_file = request.form["file"]
        variables = request.form["variables"]
        context = json.loads(variables)
        execfile(script_file, context)
        result["result"] = unicode(context["result"])
        return json.dumps(result, False)
    except Exception, e:
        result["success"] = False
        result["errorMsg"] = traceback.format_exc(e)
        return json.dumps(result, False)


if __name__ == "__main__":
    manager.run()
